<?php

namespace App\Http\Controllers;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Laravel\Socialite\Facades\Socialite;
use GuzzleHttp\Client;

class HomeController
{
    public function client()
    {
        $xml =
            <<<XML
<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">
<voice name="zh-CN-XiaoxiaoNeural">
<prosody rate="0%" pitch="0%">
张露露，我爱你。你的腿腿好软啊
</prosody>
</voice>
</speak>
XML;
        $url = 'https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1';
        $token = 'eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJyZWdpb24iOiJlYXN0YXNpYSIsInN1YnNjcmlwdGlvbi1pZCI6Ijc0MjIxNzhjZDYyZjQyMmZiZWRhOTBkMzQ0NTE4ODNjIiwicHJvZHVjdC1pZCI6IlNwZWVjaFNlcnZpY2VzLkYwIiwiY29nbml0aXZlLXNlcnZpY2VzLWVuZHBvaW50IjoiaHR0cHM6Ly9hcGkuY29nbml0aXZlLm1pY3Jvc29mdC5jb20vaW50ZXJuYWwvdjEuMC8iLCJhenVyZS1yZXNvdXJjZS1pZCI6Ii9zdWJzY3JpcHRpb25zLzVlMDVkMGI3LThlZTMtNGFhNy05MjFlLTFkMTUwNGE1ZjMzMS9yZXNvdXJjZUdyb3Vwcy_pm4blm6Lor63pn7MvcHJvdmlkZXJzL01pY3Jvc29mdC5Db2duaXRpdmVTZXJ2aWNlcy9hY2NvdW50cy9yZWJlY2NhLWFzaWEtaG9uZ2tvbmciLCJzY29wZSI6InNwZWVjaHNlcnZpY2VzIiwiYXVkIjoidXJuOm1zLnNwZWVjaHNlcnZpY2VzLmVhc3Rhc2lhIiwiZXhwIjoxNjQzOTcwMTQ3LCJpc3MiOiJ1cm46bXMuY29nbml0aXZlc2VydmljZXMifQ.cVe3U3wS0dXJYaxXIq7NXoCdqw1fjAKfTqu5yq_C9w0';
        $client = new Client();
        try {
            try {
                $res = $client->post($url,
                    [
                        'body'    => $xml,
                        'headers' => [
                            'Content-Type' => 'application/ssml+xml',
                            'ACCEPT'       => 'application/xml',
                            'Ocp-Apim-Subscription-Key' => 'd2237069265d4f5cb185ff9bf8e8688f',
                            'Authorization' => 'Bearer '.$token,
                            'X-Microsoft-OutputFormat' => 'audio-16khz-128kbitrate-mono-mp3',
                        ],
                        'verify' => false
                    ]);
            } catch (GuzzleException $e) {
                dump($e);
            }
            $response = $res->getBody()->getContents();
            Storage::put('test1.mp3',$response);
            dump('成功');
//            dd($response);
            return '成功';

            $xml_str = simplexml_load_string($response);//返回string的xml转为xml


            $json = json_decode(json_encode($xml_str->xpath('//item')), true);

            return $json;
        }catch (\Exception $e){
            dump($e);
            return 'error';


        }

    }
    public function index()
    {
        //https://INSERT_REGION_HERE.tts.speech.microsoft.com/cognitiveservices/v1
        $url = 'https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1';
        $curl = curl_init();
        $xml =
            <<<XML
<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">
<voice name="zh-CN-XiaoxiaoNeural">
<prosody rate="0%" pitch="0%">
你可将此文本替换为所需的任何文本。你可在此文本框中编写或在此处粘贴你自己的文本。
试用不同的语言和声音。改变语速和音调。你甚至可调整 SSML（语音合成标记语言），以控制文本不同部分的声音效果。单击上面的 SSML 试用一下！
请尽情使用文本转语音功能！
</prosody>
</voice>
</speak>
XML;

        $params = $xml;

        $token = 'eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJyZWdpb24iOiJlYXN0YXNpYSIsInN1YnNjcmlwdGlvbi1pZCI6Ijc0MjIxNzhjZDYyZjQyMmZiZWRhOTBkMzQ0NTE4ODNjIiwicHJvZHVjdC1pZCI6IlNwZWVjaFNlcnZpY2VzLkYwIiwiY29nbml0aXZlLXNlcnZpY2VzLWVuZHBvaW50IjoiaHR0cHM6Ly9hcGkuY29nbml0aXZlLm1pY3Jvc29mdC5jb20vaW50ZXJuYWwvdjEuMC8iLCJhenVyZS1yZXNvdXJjZS1pZCI6Ii9zdWJzY3JpcHRpb25zLzVlMDVkMGI3LThlZTMtNGFhNy05MjFlLTFkMTUwNGE1ZjMzMS9yZXNvdXJjZUdyb3Vwcy_pm4blm6Lor63pn7MvcHJvdmlkZXJzL01pY3Jvc29mdC5Db2duaXRpdmVTZXJ2aWNlcy9hY2NvdW50cy9yZWJlY2NhLWFzaWEtaG9uZ2tvbmciLCJzY29wZSI6InNwZWVjaHNlcnZpY2VzIiwiYXVkIjoidXJuOm1zLnNwZWVjaHNlcnZpY2VzLmVhc3Rhc2lhIiwiZXhwIjoxNjQzOTYxODQ4LCJpc3MiOiJ1cm46bXMuY29nbml0aXZlc2VydmljZXMifQ.f0dIkOzfMDfEVZSCWxPnisHdBxuqqlDTJTA2mdhxq80';
        $myApp_name = 'curl';
        //"X-Microsoft-OutputFormat: raw-16khz-16bit-mono-pcm",
        $header = [
            "Ocp-Apim-Subscription-Key: d2237069265d4f5cb185ff9bf8e8688f",
            "Authorization: Bearer ".$token,
            "Content-Type: application/ssml+xml",
            "X-Microsoft-OutputFormat: audio-16khz-128kbitrate-mono-mp3",
            "User-Agent: $myApp_name",
            "Content-Length:". strlen($params)
        ];


        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

        $response = curl_getinfo($curl);

        dump('123',$response);
        //
//        return Socialite::driver('azure')->redirect();

        return 'ceshi';
//
//        $t1 = \Illuminate\Support\Facades\DB::table('t_ali_order')
//            ->whereIn('order_status',['FINISH','WAIT_BUYER_ACCEPT_GOODS'])
//            ->whereIn('order_end_reason',['buyer_confirm_goods','buyer_confirm_goods_timeout'])
//            ->whereDate('gmt_create','>','2021-6-30')
//            ->select(['init_oder_amount','gmt_create','shop_code'])
//            ->get();
//        Cache::put('allOrder',$t1);
//        Cache::put('shop_code',DB::table('t_ali_order')->groupBy('shop_code')->pluck('shop_code'));
//        dd();
        $allShop =Cache::get('shop_code');
        $allOrder = Cache::get('allOrder');
        foreach ($allShop as $value){
            dump($value);
            dump(collect($allOrder)->where('shop_code',$value)->count());
//            dd('完成');

        }

//        $allOrder->where('gmt_create','>','2021-6-30');
//        $a = $t1->whereDate('gmt_create','>','2020-12-31');
//        dump($a);
//        dump($t1);
    }
}
